﻿using System; 
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;
using Assessment.DBUtility;
namespace Assessment.DAL  
{
	 	//Sys_Module
		public partial class Sys_ModuleDAL
	{
   		     
		public bool Exists(int ModuleId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Sys_Module");
			strSql.Append(" where ");
			                                       strSql.Append(" ModuleId = @ModuleId  ");
                            			SqlParameter[] parameters = {
					new SqlParameter("@ModuleId", SqlDbType.Int,4)
			};
			parameters[0].Value = ModuleId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}
		
				
		
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Assessment.Model.Sys_Module model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Sys_Module(");			
            strSql.Append("ModuleName,ModuleCode,ParentId,Valid,Href,IsLeaf,OrderNo");
			strSql.Append(") values (");
            strSql.Append("@ModuleName,@ModuleCode,@ParentId,@Valid,@Href,@IsLeaf,@OrderNo");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");		
			SqlParameter[] parameters = {
			            new SqlParameter("@ModuleName", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@ModuleCode", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@ParentId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Valid", SqlDbType.Int,4) ,            
                        new SqlParameter("@Href", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@IsLeaf", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderNo", SqlDbType.VarChar,10)             
              
            };
			            
            parameters[0].Value = model.ModuleName;                        
            parameters[1].Value = model.ModuleCode;                        
            parameters[2].Value = model.ParentId;                        
            parameters[3].Value = model.Valid;                        
            parameters[4].Value = model.Href;                        
            parameters[5].Value = model.IsLeaf;                        
            parameters[6].Value = model.OrderNo;                        
			   
			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);			
			if (obj == null)
			{
				return 0;
			}
			else
			{
				                    
            	return Convert.ToInt32(obj);
                                                                  
			}			   
            			
		}
		
		
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Assessment.Model.Sys_Module model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Sys_Module set ");
			                                                
            strSql.Append(" ModuleName = @ModuleName , ");                                    
            strSql.Append(" ModuleCode = @ModuleCode , ");                                    
            strSql.Append(" ParentId = @ParentId , ");                                    
            strSql.Append(" Valid = @Valid , ");                                    
            strSql.Append(" Href = @Href , ");                                    
            strSql.Append(" IsLeaf = @IsLeaf , ");                                    
            strSql.Append(" OrderNo = @OrderNo  ");            			
			strSql.Append(" where ModuleId=@ModuleId ");
						
SqlParameter[] parameters = {
			            new SqlParameter("@ModuleId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ModuleName", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@ModuleCode", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@ParentId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Valid", SqlDbType.Int,4) ,            
                        new SqlParameter("@Href", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@IsLeaf", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderNo", SqlDbType.VarChar,10)             
              
            };
						            
            parameters[0].Value = model.ModuleId;                        
            parameters[1].Value = model.ModuleName;                        
            parameters[2].Value = model.ModuleCode;                        
            parameters[3].Value = model.ParentId;                        
            parameters[4].Value = model.Valid;                        
            parameters[5].Value = model.Href;                        
            parameters[6].Value = model.IsLeaf;                        
            parameters[7].Value = model.OrderNo;                        
            int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ModuleId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Sys_Module ");
			strSql.Append(" where ModuleId=@ModuleId");
						SqlParameter[] parameters = {
					new SqlParameter("@ModuleId", SqlDbType.Int,4)
			};
			parameters[0].Value = ModuleId;


			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string ModuleIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Sys_Module ");
			strSql.Append(" where ID in ("+ModuleIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
				
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Assessment.Model.Sys_Module GetModel(int ModuleId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ModuleId, ModuleName, ModuleCode, ParentId, Valid, Href, IsLeaf, OrderNo  ");			
			strSql.Append("  from Sys_Module ");
			strSql.Append(" where ModuleId=@ModuleId");
						SqlParameter[] parameters = {
					new SqlParameter("@ModuleId", SqlDbType.Int,4)
			};
			parameters[0].Value = ModuleId;

			
			Assessment.Model.Sys_Module model=new Assessment.Model.Sys_Module();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			
			if(ds.Tables[0].Rows.Count>0)
			{
												if(ds.Tables[0].Rows[0]["ModuleId"].ToString()!="")
				{
					model.ModuleId=int.Parse(ds.Tables[0].Rows[0]["ModuleId"].ToString());
				}
																																				model.ModuleName= ds.Tables[0].Rows[0]["ModuleName"].ToString();
																																model.ModuleCode= ds.Tables[0].Rows[0]["ModuleCode"].ToString();
																												if(ds.Tables[0].Rows[0]["ParentId"].ToString()!="")
				{
					model.ParentId=int.Parse(ds.Tables[0].Rows[0]["ParentId"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["Valid"].ToString()!="")
				{
					model.Valid=int.Parse(ds.Tables[0].Rows[0]["Valid"].ToString());
				}
																																				model.Href= ds.Tables[0].Rows[0]["Href"].ToString();
																												if(ds.Tables[0].Rows[0]["IsLeaf"].ToString()!="")
				{
					model.IsLeaf=int.Parse(ds.Tables[0].Rows[0]["IsLeaf"].ToString());
				}
																																				model.OrderNo= ds.Tables[0].Rows[0]["OrderNo"].ToString();
																										
				return model;
			}
			else
			{
				return null;
			}
		}
		
		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM Sys_Module ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" * ");
			strSql.Append(" FROM Sys_Module ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

   
	}
}


